const tool = require('./tool.js');
var params = {};
//一轮评论多少个视频
maxplNum = 30
//随机停留的时间
randomTime = 15
//重复进入视频的次数
var enterVedioCount = 0;
//评论失败的次数
var failCount = 0;
function main(p) {
    params = p;
    init()
}

//启动抖音
function init() {
    tool.restartApp("抖音")
    sleep(5000)
    tool.log("开始搜索")
    if (press(1200, 200, 10)) {
        tool.log("点击搜索成功")
        sleep(2000)
        tool.log("搜索词：" + params.searchWord)
        setText(random(1, 10) + params.searchWord + random(1, 10))
        sleep(500)
        tool.clickText("搜索")
        sleep(2000)
        enterSearchVideo()
    } else {
        tool.log("点击搜索失败")
        init()
    }
}

//进入搜索视频
function enterSearchVideo() {
    if (tool.clickTextByScreenCapture('视频')) {
        sleep(3000)
        press(1160, 333, 10)
        sleep(1000)
        if (tool.clickTextByScreenCapture('最新发布')) {
        }
        if (tool.clickTextByScreenCapture('还未看过')) {
            sleep(3000)
            press(300, 2000, 10)
        }
        sleep(3000)
        press(420, 750, 10)
        pinglun()
    } else {
        let p = tool.getText("综合")
        if (p) {
            tool.log("移动综合")
            p = p['bounds']
            swipe(p.left + 300, p.top, p.left, p.top, 300)
            enterSearchVideo()
        } else {
            tool.log("综合未找到")
            init()
        }
    }
}


//开始评论
function pinglun() {
    tool.log("开始评论")
    for (let i = 0; i < maxplNum; i++) {
        // let p = findpinglun()
        // if (p) {
        if (tool.isChange()) {
            tool.log("当前视频未变化")
            enterVedioCount++
            if (enterVedioCount >= 2) {
                tool.log("评论次数过多，退出评论")
                enterVedioCount = 0
                init()
                return
            }
        } else {
            enterVedioCount = 0
            if (sendPinglun(params.pinglun)) {
                tool.log("评论成功")
                failCount = 0
                sleep(1000, randomTime * 1000)
            } else {
                tool.log("评论失败")
                failCount++
                if (failCount >= 3) {
                    failCount = 0
                    init()
                    return
                }
            }
            press(800,1500,100)
            sleep(200)
        }
        //截取当前屏幕
        captureScreen(`./image/dqpm.png`);
        nextLive(3000)
    }
    init()
}



//查找评论图标
function findpinglun() {
    let p = null;
    let option = {
        "region": [device.width / 5 * 4, device.height / 2, device.width / 5, device.height / 4],
        "threshold": 0.5
    }
    for (let i = 0; i < 5; i++) {
        p = tool.findImage( "pl.png", option)
        if (p) {
            break;
        }
    }
    return p;
}

//发送评论
function sendPinglun(text) {
    sleep(200)
    if(click("善语结善缘，恶言伤人心")){
        tool.log("打开评论")
        sleep(500)
        setText(text)    //输入评论
        sleep(500)
        if (click("发送")) {      //发送评论
            sleep(500)
            return true;
        } else {
            return false;
        }
    }else{
        return false;
    }
    
  
}

//切换下一个直播间
function nextLive(time) {
    tool.log("切换下一个直播间")
    sleep(1000);
    let startX = random(200, 300)
    let startY = random(1500, 1600)
    let endX = random(300, 350)
    let endY = random(50, 100)
    swipe(startX, startY, endX, endY, 50)
    sleep(time)
}


exports.main = main;